<template>
  <div>
    <!-- 轮播 -->
    <div>
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="(img, index) in imageUrl" :key="index">
          <img :src="img.imageUrl" width="100%" alt="" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <!-- 推荐歌单 -->
    <div>
      <p class="title">推荐歌单</p>
      <ul >
        <li v-for="(item, index) in songList" :key="index" @click="goSonglist(item.id)" class="songlist">
          <img :src="item.picUrl" width="118px" />
          <p class="listname">{{ item.name }}</p>
        </li>
      </ul>
    </div>
    <!-- 最新音乐 -->
    <div>
      <p class="title">最新音乐</p>
      <div
        v-for="(item, index) in newSongList"
        :key="index"
        class="newsonglist"
        @click="play(item.id)"
      >
        <p>{{ item.name }}</p>
        <p class="artists">
          <i class="sq"></i>
          <span v-for="(artist,idx) in item.song.artists"
          :key="'artist' + idx">
              {{artist.name}}
            <span v-if="item.song.artists.length>1 && idx!= item.song.artists.length-1">/</span>
          </span>
        </p>
        <!-- 播放按钮 -->
        <span class="play"></span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      imageUrl: [],
      // 歌单的数组
      songList: [],
      // 最新音乐的数组
      newSongList: [],
    };
  },
  created() {
    // 获取轮播图的数据
    this.getBanner();
    // 获取歌单的数据
    this.getSongList();
    // 获取最新歌曲的数据
    this.getNewSong();
  },
  methods: {
    getBanner() {
      this.$http
        .get("/banner")
        .then((res) => {
          this.imageUrl = res.data.banners;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 歌单
    getSongList() {
      this.$http("/personalized?limit=6")
        .then((res) => {
          this.songList = res.data.result;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getNewSong() {
      this.$http
        .get("/personalized/newsong")
        .then((res) => {
          this.newSongList = res.data.result;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    goSonglist(id){
      this.$router.push('/songlist/' + id);
    },
    // 跳转到播放页面
    play(id) {
      this.$router.push("/play/" + id);
    },
  },
};
</script>

<style scoped>
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  line-height: 10px;
  text-align: center;
  background-color: #39a9ed;
}
.title {
  padding-left: 10px;
  border-left: 2px solid #f00;
  font-size: 18px;
}
.songlist {
  list-style: none;
  display: inline-block;
  width: 32%;
  margin-left: 5px;
}
.listname {
  display: -webkit-box;
  /* 文字总数为两行 */
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  /* 超出文字用省略号代替 */
  text-overflow: ellipsis;
  /* 超出部分隐藏 */
  overflow: hidden;
}
.newsonglist {
  height: 76px;
  border-bottom: 1px solid #f4f4f4;
  font-size: 18px;
  padding-left: 10px;
  position: relative;
}
.artists {
  color: #666;
  font-size: 14px;
}
.sq {
  display: inline-block;
  width: 14px;
  height: 10px;
  background-image: url("../../static/img/index_icon_2x.png");
  background-position: 0 0;
  background-size: 190px 113px;
}
.play {
  width: 45px;
  height: 45px;
  background-image: url(/static/img/index_icon_2x.5207a28.png);
  background-repeat: no-repeat;
  background-position: -47px 1px;
  position: absolute;
  right: 30px;
  top: 6px;
}
</style>